<?php
class CartController extends DefaultModuleController {	
	public function actionIndex() {
		if(!$this->userId){
			$this->message('你还没有登录，请先登录。', '/site-login.htm');
		}
		/*
		echo "<script>alert('施耐德产品价格上调，暂不能订货，价格更新中');history.go(-1);</script>";
		*/
		$action = getgpc('action', 'G');
		if($action && ($action=='add' || $action=='less' || $action=='update' || $action=='del' || $action=='clear')) {
			$productId = (int)getgpc('productId', 'G');
			if($cartArr = $this->ShoppingcartModel->find("userId = '$this->userId' AND productId='$productId' AND settleStatus=0")) {
				if($action=='less' && $cartArr['cartCount'] > 1) {
					$this->ShoppingcartModel->update("userId = '$this->userId' AND productId='$productId' AND settleStatus='0'", array('cartCount'=>'-1'));
				} elseif($action=='add') {
					$this->ShoppingcartModel->update("userId = '$this->userId' AND productId='$productId' AND settleStatus='0'", array('cartCount'=>'+1'));
				} elseif($action=='update') {
					$value = getgpc('value', 'G');
					if(is_numeric($value) && $value > 0) {
						$this->ShoppingcartModel->update("userId = '$this->userId' AND productId='$productId' AND settleStatus='0'", array('cartCount'=>$value));
					}
				}
			}
			if($action=='del') {
				$this->ShoppingcartModel->delete("userId = '$this->userId' AND productId='$productId' AND settleStatus=0");
			} elseif($action=='clear') {
				$cartId = getgpc('cartId', 'G');
				if($cartId && is_numeric(str_replace('-','',$cartId))) {
					$this->ShoppingcartModel->delete("userId = '$this->userId' AND id IN(". str_replace('-',',',rtrim($cartId,'-')) .") AND settleStatus=0");
				}
			}
			exit(json_encode(array('state'=>'1','pr'=>$this->_totalPrice())));
		}

		$this->view->assign('pArr', $this->_totalPrice());
		$this->display('my_cart');
	}

	public function actionConsignee() {
		$action = getgpc('action','G');		
		if($action && $action=='distributionUpdate'){
			$id = (int)getgpc('id', 'G');
			if(isset($_POST['info']) && is_array($_POST['info'])) {
				$infoArr = getgpc('info','P');
				$infoArr['city'] = $_POST['user']['areaId'];
				$infoArr['userId'] = $this->userId;
				$infoArr['createdTime'] = $this->time;
				$insertId = $this->ConsigneeModel->update("id='$id'", $infoArr);
			}
			exit(json_encode(array('state'=>1,'insertId'=>$id)));
		}
		
		$consigneeCount = $this->ConsigneeModel->count("userId = '$this->userId'");
		if($consigneeCount == 0) {		
			if(isset($_POST['info']) && is_array($_POST['info'])) {
				$infoArr = getgpc('info','P');
				$infoArr['city'] = $_POST['user']['areaId'];
				$infoArr['userId'] = $this->userId;
				$this->ConsigneeModel->insert($infoArr);
				$this->redirect('cart-buynow.htm');
			}
		} elseif($consigneeCount > 0) {
			$this->db->query("INSERT INTO god_consignee(userId,realName,email,province,city,address,zipcode,phone,mobile,signBuilding,bestTime,createdTime) SELECT userId,realName,email,province,city,address,zipcode,phone,mobile,signBuilding,bestTime,createdTime FROM god_consignee WHERE userId = '$this->userId' ORDER BY id DESC LIMIT 1");
			$this->redirect('cart-buynow.htm');
		}

		$this->view->assign('areaList', $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC"));
		$this->view->assign('info', $this->UserModel->find("userId='$this->userId'"));
		$this->display('my_consignee');
	}

	public function actionBuynow() {
		$consigneeArr = $this->ConsigneeModel->find("userId = '$this->userId' ORDER BY id DESC LIMIT 1");
		$consigneeArr['cityInfo'] = $this->AreaModel->getArea($consigneeArr['city']);

		if(isset($_POST['step'], $_POST['cartId'], $_POST['logisticId'], $_POST['payType'], $_POST['totalPrice']) && $_POST['step']=='done') {
			$cartId = implode(',', getgpc('cartId','P'));
			
			if($_POST['payType']==1 && $_POST['totalPrice']>GODHOUSE_PAY_LIMITAMOUNT) {
				$this->alert('总金额超过'.GODHOUSE_PAY_LIMITAMOUNT.'元，请选择银行汇款/转帐', 'BACK');
			}
			
			if($this->ShoppingcartModel->oneColumn("id IN ($cartId) LIMIT 1", 'settleStatus')==0) {
				$orderNo = 'Brt_'.date('ymdHis');
				
				$invoice = getgpc('invoice', 'P');
				$invoice['orderId'] = $orderNo;
				$inse = $this->InvoiceModel->insert($invoice);

				
				if($orderId = $this->OrderModel->insert(array(
					'userId'=>$this->userId,
					'userName'=>$this->userName,
					'orderNo'=>$orderNo,
					'consigneeId' => $consigneeArr['id'],
					'logisticId'=>getgpc('logisticId','P'),
					'payType'=>getgpc('payType','P'),
					'remark'=>getgpc('remark','P'),
					'totalPrice'=>getgpc('totalPrice','P'),
					'createdTime'=>$this->time,
					'adminId'=>$this->user['parentId'],
				))){
					$this->ShoppingcartModel->update("id IN (". $cartId .")", array('settleStatus'=>1, 'orderId'=>$orderId));
					if($_POST['payType']==1) {
						$this->redirect("/cart-alipay.htm?orderNo=$orderNo&totalPrice=".getgpc('totalPrice','P'));
					} else {
						$this->redirect('/order.htm');
					}
				}
			} else {
				$this->message('你的订单已经提交了，请不要重复提交', '/order.htm');
			}
		}
		
		$this->view->assign('areaList', $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC"));
		$this->view->assign('info', $consigneeArr);
		$this->view->assign('logisticArr', $this->LogisticModel->fetchAll());
		$this->view->assign('CartArr', $this->_totalPrice());
		$this->display('my_buynow');
	}
	
	public function actionAlipay() {
		
		$this->display('my_alipay');
	}
}

?>